<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title>添加药品</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="/layui/css/layui.css" rel="stylesheet">
</head>
<body>
<div class="layui-margin-3 layui-anim layui-anim-upbit" style="margin-top: 15px;">
    <div class="layui-row">
        <div class="layui-col-md12">
            <form class="layui-form layui-form-pane">
                <div class="layui-form-item layui-col-space20">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">药品名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" autocomplete="off" class="layui-input" lay-verify="required">
                        </div>
                    </div>

                    <div class="layui-col-md6">
                        <label class="layui-form-label">国药准字</label>
                        <div class="layui-input-block">
                            <input type="text" name="code" autocomplete="off" class="layui-input" lay-verify="required">
                        </div>
                    </div>

                    <div class="layui-col-md6">
                        <label class="layui-form-label">药品规格</label>
                        <div class="layui-input-block">
                            <input type="text" name="specs" autocomplete="off" class="layui-input" lay-verify="required">
                        </div>
                    </div>

                    <div class="layui-col-md6">
                        <label class="layui-form-label">用法用量</label>
                        <div class="layui-input-block">
                            <input type="text" name="usage" autocomplete="off" class="layui-input" lay-verify="required">
                        </div>
                    </div>

                    <div class="layui-col-md6">
                        <label class="layui-form-label">包装照片</label>
                        <div class="layui-input-block">
                            <input type="text" name="image" autocomplete="off" class="layui-input" lay-verify="required">
                        </div>
                    </div>

                    <div class="layui-col-md6">
                        <label class="layui-form-label">生产厂家</label>
                        <div class="layui-input-block">
                            <input type="text" name="origin" autocomplete="off" class="layui-input" lay-verify="required">
                        </div>
                    </div>

                    <div class="layui-col-md12 layui-form-text">
                        <label class="layui-form-label">药品成分</label>
                        <div class="layui-input-block">
                            <textarea name="compose" class="layui-textarea" lay-verify="required" maxlength="1000" rows="8"></textarea>
                        </div>
                    </div>

                </div>

                <div class="layui-form-item layui-col-space20">
                    <div class="layui-col-md12" style="text-align: center;">
                        <button class="layui-btn" lay-submit lay-filter="toDo">添加</button>
                        <button type="reset" class="layui-btn layui-btn-primary">清空</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="/layui/layui.js"></script>
<script>
    layui.use(['form', 'layer', 'jquery'], function () {
        let form = layui.form;
        let layer = layui.layer;
        let $ = layui.jquery;

        // 提交事件
        form.on('submit(toDo)', function (data) {
            let field = data.field; // 获取表单字段值

            $.ajax({
                url: '/doAddMedicine',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(field),
                success: function (res) {
                    if (res.code) {
                        layer.msg('添加成功！', {icon: 1});
                        setTimeout(function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));    // 关闭弹出层
                        }, 2000);
                    } else {
                        layer.msg('添加失败！', {icon: 2});
                    }
                },
                error: function (err) {
                    layer.msg('Ajax请求异常！', {icon: 0});
                }
            });
            return false; // 阻止默认 form 跳转
        });
    });
</script>
</body>
</html>